package com.example.ad.sdk.domain.repositories.interceptor

import com.example.ad.sdk.domain.utils.AppDeviceUtil
import okhttp3.Interceptor
import okhttp3.Response

/**
 * 添加api网络请求信息头
 */
class HeaderInterceptor : Interceptor {

    /**
     * app相关信息头
     */
    private fun appInfoHeader(): String{
        val stringBuilder = StringBuilder()
        stringBuilder
            .append(AppDeviceUtil.deviceBrand())
            .append("(")
            .append(AppDeviceUtil.systemModel())
            .append("),Android ")
            .append(AppDeviceUtil.systemVersion())
            .append(",")
            .append(AppDeviceUtil.networkType())
        return stringBuilder.toString()
    }

    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request().newBuilder()
            .addHeader("Accept", "application/json")
            .removeHeader("User-Agent")
            .addHeader("User-Agent", appInfoHeader())
            .build()
        return chain.proceed(request)
    }

}